/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = t__resp_parse.c                                    */
/*  NOTE      =                                                    */
/*  DATE      = 2016/03/31 by jikw                                 */
/*******************************************************************/

#include <CUnit/Basic.h>
#include <stdlib.h>

#include <redis/redis_core.h>
#include "frsutl.h"
#include "frcomm.h"
#include "frmem.h"
#include "conntrack.h"
#include "frct_comm.h"
#include "frreport_comm.h"
#include "../../../src/module/redis/config.h"
#include "frbyte_buffer.h"
#include "apmtest_cunit.h"

typedef struct __st_L7session_redis_private
{
    unsigned long ul_seq;                             /*!< 序列号，1次req和response时，使用同一个序列号 */
    L4_transaction st_l4_trans;                       /*!< 数据 */
    Frreport_body_data *p_st_body_data_req;
    Frreport_body_data *p_st_body_data_resp;
    Frlist_header *p_lst_comm;
    Frlist_header *p_lst_req;
    Frbyte_Packet *p_resp_packet;                     /*!< 存相应bytebuff数据 */
}L7session_redis_private;

extern int __resp_parse(Frbyte_Packet *p_packet, L7session_redis_private *p_st_private, Frreport_body_data *p_st_body_data, Conntrack *p_st_conn);
extern int redis_session_destroy(void *p_private_session);

static int __suite_init(void)
{

	return 0;
}

static int __suite_cleanup(void)
{

	return 0;
}

void case1(void)
{
    Frbyte_Packet *p_packet = NULL;
    L7session_redis_private *p_st_private = NULL;
    Frreport_body_data *p_st_body_data = NULL;
    Conntrack *p_st_conn = NULL;

    int i_ret = 0;
    i_ret = __resp_parse(p_packet, p_st_private, p_st_body_data, p_st_conn);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_FAIL);

    return;
}

void case2(void)
{
    int i_ret = 0;
    char *p_data = NULL;
    Frbyte_Packet *p_packet = NULL;
    L7session_redis_private *p_st_private = NULL;
    Frreport_body_data *p_st_body_data = NULL;
    Conntrack *p_st_conn = NULL;

    p_data = (char *)":1\r\n";
    p_packet = bytebuff_creat();
    if(p_packet == NULL)
        goto ERROR;
    if(bytebuff_add(p_packet, p_data, 4) != FR_SUCC)
        goto ERROR;

    p_st_private = (L7session_redis_private *)frmalloc(sizeof(L7session_redis_private));
    if(p_st_private == NULL)
        goto ERROR;
    memset(p_st_private, 0x00, sizeof(L7session_redis_private));

    p_st_body_data = (Frreport_body_data *)frmalloc(sizeof(Frreport_body_data));
    if(p_st_body_data == NULL)
        goto ERROR;
    memset(p_st_body_data, 0x00, sizeof(Frreport_body_data));
    p_st_body_data->em_type = FRREPORT_TYPE_L7DATA;

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    if(p_st_conn == NULL)
        goto ERROR;
    memset(p_st_conn, 0x00, sizeof(Conntrack));

    i_ret = __resp_parse(p_packet, p_st_private, p_st_body_data, p_st_conn);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SET_RESP(APM_L7_ANALYZE_SUCC));

    redis_session_destroy(p_st_private);
    frreport_body_free(&p_st_body_data);
    frfree(p_st_conn);
    bytebuff_free(&p_packet);

    return;

ERROR:
    if(p_st_private)
        redis_session_destroy(p_st_private);
    if(p_st_body_data)
        frreport_body_free(&p_st_body_data);
    if(p_st_conn)
        frfree(p_st_conn);
    if(p_packet)
        bytebuff_free(&p_packet);
    return;
}

void case3(void)
{
    int i_ret = 0;
    char *p_data = NULL;
    Frbyte_Packet *p_packet = NULL;
    L7session_redis_private *p_st_private = NULL;
    Frreport_body_data *p_st_body_data = NULL;
    Conntrack *p_st_conn = NULL;

    p_data = (char *)"+OK\r\n";
    p_packet = bytebuff_creat();
    if(p_packet == NULL)
        goto ERROR;
    if(bytebuff_add(p_packet, p_data, 5) != FR_SUCC)
        goto ERROR;

    p_st_private = (L7session_redis_private *)frmalloc(sizeof(L7session_redis_private));
    if(p_st_private == NULL)
        goto ERROR;
    memset(p_st_private, 0x00, sizeof(L7session_redis_private));

    p_st_body_data = (Frreport_body_data *)frmalloc(sizeof(Frreport_body_data));
    if(p_st_body_data == NULL)
        goto ERROR;
    memset(p_st_body_data, 0x00, sizeof(Frreport_body_data));
    p_st_body_data->em_type = FRREPORT_TYPE_L7DATA;

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    if(p_st_conn == NULL)
        goto ERROR;
    memset(p_st_conn, 0x00, sizeof(Conntrack));

    i_ret = __resp_parse(p_packet, p_st_private, p_st_body_data, p_st_conn);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SET_RESP(APM_L7_ANALYZE_SUCC));

    redis_session_destroy(p_st_private);
    frreport_body_free(&p_st_body_data);
    frfree(p_st_conn);
    bytebuff_free(&p_packet);

    return;

ERROR:
    if(p_st_private)
        redis_session_destroy(p_st_private);
    if(p_st_body_data)
        frreport_body_free(&p_st_body_data);
    if(p_st_conn)
        frfree(p_st_conn);
    if(p_packet)
        bytebuff_free(&p_packet);
    return;
}

void case4(void)
{
    int i_ret = 0;
    char *p_data = NULL;
    Frbyte_Packet *p_packet = NULL;
    L7session_redis_private *p_st_private = NULL;
    Frreport_body_data *p_st_body_data = NULL;
    Conntrack *p_st_conn = NULL;

    p_data = (char *)"$4\r\n\x01\x02\x03\x04\r\n";
    p_packet = bytebuff_creat();
    if(p_packet == NULL)
        goto ERROR;
    if(bytebuff_add(p_packet, p_data, 10) != FR_SUCC)
        goto ERROR;

    p_st_private = (L7session_redis_private *)frmalloc(sizeof(L7session_redis_private));
    if(p_st_private == NULL)
        goto ERROR;
    memset(p_st_private, 0x00, sizeof(L7session_redis_private));

    p_st_body_data = (Frreport_body_data *)frmalloc(sizeof(Frreport_body_data));
    if(p_st_body_data == NULL)
        goto ERROR;
    memset(p_st_body_data, 0x00, sizeof(Frreport_body_data));
    p_st_body_data->em_type = FRREPORT_TYPE_L7DATA;

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    if(p_st_conn == NULL)
        goto ERROR;
    memset(p_st_conn, 0x00, sizeof(Conntrack));

    i_ret = __resp_parse(p_packet, p_st_private, p_st_body_data, p_st_conn);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SET_RESP(APM_L7_ANALYZE_SUCC));

    redis_session_destroy(p_st_private);
    frreport_body_free(&p_st_body_data);
    frfree(p_st_conn);
    bytebuff_free(&p_packet);

    return;

ERROR:
    if(p_st_private)
        redis_session_destroy(p_st_private);
    if(p_st_body_data)
        frreport_body_free(&p_st_body_data);
    if(p_st_conn)
        frfree(p_st_conn);
    if(p_packet)
        bytebuff_free(&p_packet);
    return;
}

void case5(void)
{
    int i_ret = 0;
    char *p_data = NULL;
    Frbyte_Packet *p_packet = NULL;
    L7session_redis_private *p_st_private = NULL;
    Frreport_body_data *p_st_body_data = NULL;
    Conntrack *p_st_conn = NULL;

    p_data = (char *)"$-1\r\n";
    p_packet = bytebuff_creat();
    if(p_packet == NULL)
        goto ERROR;
    if(bytebuff_add(p_packet, p_data, 5) != FR_SUCC)
        goto ERROR;

    p_st_private = (L7session_redis_private *)frmalloc(sizeof(L7session_redis_private));
    if(p_st_private == NULL)
        goto ERROR;
    memset(p_st_private, 0x00, sizeof(L7session_redis_private));

    p_st_body_data = (Frreport_body_data *)frmalloc(sizeof(Frreport_body_data));
    if(p_st_body_data == NULL)
        goto ERROR;
    memset(p_st_body_data, 0x00, sizeof(Frreport_body_data));
    p_st_body_data->em_type = FRREPORT_TYPE_L7DATA;

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    if(p_st_conn == NULL)
        goto ERROR;
    memset(p_st_conn, 0x00, sizeof(Conntrack));

    i_ret = __resp_parse(p_packet, p_st_private, p_st_body_data, p_st_conn);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SET_RESP(APM_L7_ANALYZE_SUCC));

    redis_session_destroy(p_st_private);
    frreport_body_free(&p_st_body_data);
    frfree(p_st_conn);
    bytebuff_free(&p_packet);

    return;

ERROR:
    if(p_st_private)
        redis_session_destroy(p_st_private);
    if(p_st_body_data)
        frreport_body_free(&p_st_body_data);
    if(p_st_conn)
        frfree(p_st_conn);
    if(p_packet)
        bytebuff_free(&p_packet);
    return;
}

void case6(void)
{
    int i_ret = 0;
    char *p_data = NULL;
    Frbyte_Packet *p_packet = NULL;
    L7session_redis_private *p_st_private = NULL;
    Frreport_body_data *p_st_body_data = NULL;
    Conntrack *p_st_conn = NULL;

    p_data = (char *)"$14\r\nwww.google.com\r\n";
    p_packet = bytebuff_creat();
    if(p_packet == NULL)
        goto ERROR;
    if(bytebuff_add(p_packet, p_data, 21) != FR_SUCC)
        goto ERROR;

    p_st_private = (L7session_redis_private *)frmalloc(sizeof(L7session_redis_private));
    if(p_st_private == NULL)
        goto ERROR;
    memset(p_st_private, 0x00, sizeof(L7session_redis_private));

    p_st_body_data = (Frreport_body_data *)frmalloc(sizeof(Frreport_body_data));
    if(p_st_body_data == NULL)
        goto ERROR;
    memset(p_st_body_data, 0x00, sizeof(Frreport_body_data));
    p_st_body_data->em_type = FRREPORT_TYPE_L7DATA;

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    if(p_st_conn == NULL)
        goto ERROR;
    memset(p_st_conn, 0x00, sizeof(Conntrack));

    i_ret = __resp_parse(p_packet, p_st_private, p_st_body_data, p_st_conn);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SET_RESP(APM_L7_ANALYZE_SUCC));

    redis_session_destroy(p_st_private);
    frreport_body_free(&p_st_body_data);
    frfree(p_st_conn);
    bytebuff_free(&p_packet);
    return;

ERROR:
    if(p_st_private)
        redis_session_destroy(p_st_private);
    if(p_st_body_data)
        frreport_body_free(&p_st_body_data);
    if(p_st_conn)
        frfree(p_st_conn);
    if(p_packet)
        bytebuff_free(&p_packet);
    return;
}

void case7(void)
{
    int i_ret = 0;
    char *p_data = NULL;
    Frbyte_Packet *p_packet = NULL;
    L7session_redis_private *p_st_private = NULL;
    Frreport_body_data *p_st_body_data = NULL;
    Conntrack *p_st_conn = NULL;

    p_data = (char *)"*2\r\n$14\r\nwww.google.com\r\n$14\r\nwww.taobao.com\r\n";
    p_packet = bytebuff_creat();
    if(p_packet == NULL)
        goto ERROR;
    if(bytebuff_add(p_packet, p_data, 46) != FR_SUCC)
        goto ERROR;

    p_st_private = (L7session_redis_private *)frmalloc(sizeof(L7session_redis_private));
    if(p_st_private == NULL)
        goto ERROR;
    memset(p_st_private, 0x00, sizeof(L7session_redis_private));

    p_st_body_data = (Frreport_body_data *)frmalloc(sizeof(Frreport_body_data));
    if(p_st_body_data == NULL)
        goto ERROR;
    memset(p_st_body_data, 0x00, sizeof(Frreport_body_data));
    p_st_body_data->em_type = FRREPORT_TYPE_L7DATA;

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    if(p_st_conn == NULL)
        goto ERROR;
    memset(p_st_conn, 0x00, sizeof(Conntrack));

    i_ret = __resp_parse(p_packet, p_st_private, p_st_body_data, p_st_conn);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SET_RESP(APM_L7_ANALYZE_SUCC));

    redis_session_destroy(p_st_private);
    frreport_body_free(&p_st_body_data);
    frfree(p_st_conn);
    bytebuff_free(&p_packet);
    return;

ERROR:
    if(p_st_private)
        redis_session_destroy(p_st_private);
    if(p_st_body_data)
        frreport_body_free(&p_st_body_data);
    if(p_st_conn)
        frfree(p_st_conn);
    if(p_packet)
        bytebuff_free(&p_packet);
    return;
}

void case8(void)
{
    int i_ret = 0;
    char *p_data = NULL;
    Frbyte_Packet *p_packet = NULL;
    L7session_redis_private *p_st_private = NULL;
    Frreport_body_data *p_st_body_data = NULL;
    Conntrack *p_st_conn = NULL;

    p_data = (char *)"*3\r\n$4\r\n\x09\x0a\x0b\x0c\r\n$4\r\n\x05\x06\x07\x08\r\n$-1\r\n";
    p_packet = bytebuff_creat();
    if(p_packet == NULL)
        goto ERROR;
    if(bytebuff_add(p_packet, p_data, 29) != FR_SUCC)
        goto ERROR;

    p_st_private = (L7session_redis_private *)frmalloc(sizeof(L7session_redis_private));
    if(p_st_private == NULL)
        goto ERROR;
    memset(p_st_private, 0x00, sizeof(L7session_redis_private));

    p_st_body_data = (Frreport_body_data *)frmalloc(sizeof(Frreport_body_data));
    if(p_st_body_data == NULL)
        goto ERROR;
    memset(p_st_body_data, 0x00, sizeof(Frreport_body_data));
    p_st_body_data->em_type = FRREPORT_TYPE_L7DATA;

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    if(p_st_conn == NULL)
        goto ERROR;
    memset(p_st_conn, 0x00, sizeof(Conntrack));

    i_ret = __resp_parse(p_packet, p_st_private, p_st_body_data, p_st_conn);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SET_RESP(APM_L7_ANALYZE_SUCC));

    redis_session_destroy(p_st_private);
    frreport_body_free(&p_st_body_data);
    frfree(p_st_conn);
    bytebuff_free(&p_packet); 
    return;

ERROR:
    if(p_st_private)
        redis_session_destroy(p_st_private);
    if(p_st_body_data)
        frreport_body_free(&p_st_body_data);
    if(p_st_conn)
        frfree(p_st_conn);
    if(p_packet)
        bytebuff_free(&p_packet);
    return;
}

int add_suite__resp_parse(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" __resp_parse()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    if (CU_add_test(pSuite, "case1", case1) == NULL ||
        CU_add_test(pSuite, "case2", case2) == NULL ||
        CU_add_test(pSuite, "case3", case3) == NULL ||
        CU_add_test(pSuite, "case4", case4) == NULL ||
        CU_add_test(pSuite, "case5", case5) == NULL ||
        CU_add_test(pSuite, "case6", case6) == NULL ||
        CU_add_test(pSuite, "case7", case7) == NULL ||
        CU_add_test(pSuite, "case8", case8) == NULL)
        return FR_FAIL;

    return FR_SUCC;
}
